package cn.ainit.sys.models.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.sys.framework.FrameworkService;
import cn.ainit.sys.menu.SessionCont;
import cn.ainit.sys.models.ModelReceiveObj;
import cn.ainit.sys.models.SingleService;
import cn.ainit.sys.query.datastore.DataStore;



@Controller
@RequestMapping("/mulRep") 

public class MultiReportController {

	private MultiReportService  multiReportService;
	@Resource
	public void setMultiReportService(MultiReportService multiReportService) {
		this.multiReportService = multiReportService;
	}
	
	private SingleService singleService;
	@Resource
	public void setSingleService(SingleService singleService) {
		this.singleService = singleService;
	}

	private FrameworkService frameworkService;
	@Resource
	public void setFrameworkService(FrameworkService frameworkService) {
		this.frameworkService = frameworkService;
	}

	/**
	 * 根据dsId（只用dsId的第一个） 查询主表的数据 只返回界面的配置
	 * @param modelReceiveObj
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-4-25 上午08:58:53
	 */
	@RequestMapping("/mulQueryFunc")
	public String mulRep(ModelReceiveObj modelReceiveObj,HttpServletRequest request,HttpSession session) throws Exception{
		try {
			MulModelObj mulModelObj=new MulModelObj();
			String dsIds = modelReceiveObj.getDsId();
			String funcId=modelReceiveObj.getFuncId();
			if(dsIds==null|| dsIds==""){
				System.out.println("SingleController L.202 加载页面的时候DsId传入有误，必须修改");
			}
			Object uid = session.getAttribute(SessionCont.uid);
			if (uid == null || "".equals(uid.toString())) {
				System.out.println("用户未登录 SingleController L.138");
			}
			//加载公用的按钮
			String butnId="";
			if (modelReceiveObj.getFlag().equals("1")) {	 //如果是1 则为查询窗口的公用的按钮
				butnId="queryButns";
			}else if(modelReceiveObj.getFlag().equals("2")){ //如果是2 则为详情或修改窗口的公用的按钮
				butnId="aiuComButns";
			}else if(modelReceiveObj.getFlag().equals("3")){   //如果是3 则是报表界面
				butnId="reportButns";
			}else{
				System.out.println("#SingleController 102 获取方式参数不对modelReceiveObj.getFlag()为空");
			}
			//加载dataStore 
			DataStore dataStore=singleService.getDataStore(funcId);
			
			//加载公用按钮
			mulModelObj.setButnList(this.singleService.combutnListRight(butnId,funcId,uid.toString()));
			
			//加载个性化的按钮
			mulModelObj.setPreButnList(this.singleService.butnListRight(funcId,funcId,uid.toString(),"1"));   
			
			//多个tab标签内的属性
			String [] mulDsIds=dsIds.split(";");
			for(int i = 0; i < mulDsIds.length; i++) {
				if(mulDsIds[i]!=null&&!"".equals((mulDsIds[i].trim()))){
					MulTabReturnObj mulTabReturnObj=new MulTabReturnObj();
					mulTabReturnObj.setDataStores(singleService.getDataStore(mulDsIds[i]));
					mulTabReturnObj.setPreButnList(this.singleService.butnListRight(mulDsIds[i],mulDsIds[i],uid.toString(),"1"));  //加载报表个性化按钮
					mulModelObj.getmObjs().add(mulTabReturnObj);
				}
			}
			mulModelObj.setDataStore(dataStore);
			mulModelObj.setDsIds(dsIds);
			mulModelObj.setFuncId(funcId);
			mulModelObj.setAinitFunc(modelReceiveObj.getAinitFunc());
			request.setAttribute("m",mulModelObj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (modelReceiveObj.getJspWebUrl() == null || modelReceiveObj.getJspWebUrl().equals("")) {
			modelReceiveObj.setJspWebUrl("/model/multiReport/mulReport");
		}
		return modelReceiveObj.getJspWebUrl();
	}
	
	
	
	
	
	@RequestMapping("/repClumns")
	@ResponseBody
	public List<List>  createPages(String dsids,HttpServletRequest request,HttpSession session){
		List<List>  columnsDsids = new ArrayList<List>();
		if(dsids != null && !dsids.equals("")){
			String[] idArr = dsids.split(";");
			for (int i = 0; i < idArr.length; i++) {
				//根据dsid获取字段数据
				List columns = new ArrayList();
				columnsDsids.add(this.multiReportService.queryColumnsBydsId(idArr[i]));
			}
		}
		return columnsDsids;
	}
	
	
	/****
	 * 加载多tab 报表的所有的列集合
	 * @param mulModelObj
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 * @author feifei
	 * @date: 2015-5-10 下午06:14:56 
	 */
	@RequestMapping("/loadRepColumns")
	@ResponseBody
	public Map<String, List> loadRepColumns(@RequestBody  MulModelObj mulModelObj,HttpServletRequest request,HttpSession session) throws Exception{
		Map<String, List> maps=new HashMap<String, List>();
		try {
			Object uid = session.getAttribute(SessionCont.uid);
			if(uid == null || "".equals(uid.toString())){
				System.out.println("用户未登录..FrameworkController L.506");
			}
			List<MulTabReturnObj> mObjs=mulModelObj.getmObjs();
			for (int i = 0; i < mObjs.size(); i++) {
				System.out.println(mObjs.get(i).getDsId());
				List columnsList=new ArrayList();
				String[] types = mObjs.get(i).getType().split(";");
				for (int j = 0; j < types.length; j++) {
					if(types[j]!=null&&!"".equals(types[j])){
						List columns = frameworkService.loadColumnsU(mObjs.get(i).getDsId(),types[j],"",uid==null?"":uid.toString());
						columnsList.add(columns);
					}
				}
				maps.put(mObjs.get(i).getDsId(),columnsList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return maps;
	}
}